from pymongo import MongoClient

import re
import json
import sys
import gensim
import codecs

if sys.stdout.encoding != 'UTF-8':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
    sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')

client = MongoClient('127.0.0.1', 27017)

# boss直聘
knx_boss_db = client['knx_boss_db']
knx_boss_position = knx_boss_db['knx_boss_position']
knx_boss_enterprise = knx_boss_db['knx_boss_enterprise']

# 拉钩职位库
knx_lagou_db = client['knx_lagou_db']
knx_lagou_position = knx_lagou_db['knx_lagou_position']
knx_lagou_enterprise = knx_lagou_db['knx_lagou_enterprise']

# 大街职位库
knx_dajie_db = client['knx_dajie_db']
knx_dajie_position = knx_dajie_db['knx_dajie_position']
knx_dajie_enterprise = knx_dajie_db['knx_dajie_enterprise']

# 前程无忧职位库
knx_qiancheng_db = client['knx_qiancheng_db']
knx_qiancheng_position = knx_qiancheng_db['knx_qiancheng_position']
knx_qiancheng_enterprise = knx_qiancheng_db['knx_qiancheng_enterprise']

# 总库
knx_all_position_db = client['knx_all_position_db']
knx_all_position_coll = knx_all_position_db['knx_all_position_coll']

knx_all_enterprise_db = client['knx_all_enterprise_db']
knx_all_enterprise_coll = knx_all_enterprise_db['knx_all_enterprise_coll']


def comp_id():
    seq = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    id = []

    for i in range(30):
        id += random.sample(seq, 1)

    return ''.join(id)


if __name__ == '__main__':
    # for db in [knx_boss_enterprise, knx_lagou_enterprise, knx_dajie_enterprise, knx_qiancheng_enterprise]:
    #     for item in db.find():
    #         if not knx_all_enterprise_coll.find_one({'name': item['name']}):
    #             knx_all_enterprise_coll.insert_one(item)
    # i = 0
    # total = knx_all_enterprise_coll.count()
    #
    # for item in knx_all_enterprise_coll.find():
    #     i += 1
    #
    #     percent = float(i) * 100 / float(total)
    #     sys.stdout.write("%.2f" % percent);
    #     sys.stdout.write("\r");
    #     sys.stdout.flush()
    #
    #     knx_all_enterprise_coll.update_one({'_id': item['_id']}, {'$set': {'compId': comp_id()}})

    # total = knx_boss_position.count() + knx_lagou_position.count() + knx_dajie_position.count() + knx_qiancheng_position.count()
    # i = 0
    #
    # for db in [knx_boss_position, knx_lagou_position, knx_dajie_position, knx_qiancheng_position]:
    #     for item in db.find():
    #         i += 1
    #
    #         percent = float(i) * 100 / float(total)
    #         sys.stdout.write("%.2f" % percent);
    #         sys.stdout.write("\r");
    #         sys.stdout.flush()
    #
    #         if knx_all_enterprise_coll.find_one({'name': item['company']}):
    #             c = knx_all_enterprise_coll.find_one({'name': item['company']})
    #             db.update_one({'_id': item['_id']}, {'$set': {'compId': c['compId']}})
    #         else:
    #             print(item['name'], item['company'], 'can not find comp')

    # total = knx_boss_position.count() + knx_lagou_position.count() + knx_dajie_position.count() + knx_qiancheng_position.count()
    # i = 0
    # print(total)
    #
    # for db in [knx_boss_position, knx_lagou_position, knx_dajie_position, knx_qiancheng_position]:
    #     i += 1
    #
    #     percent = float(i) * 100 / float(total)
    #     sys.stdout.write("%.2f" % percent)
    #     sys.stdout.write("\r");
    #     sys.stdout.flush()
    #
    #     for item in db.find():
    #         knx_all_position_coll.insert_one(item)

    for db in [knx_boss_position, knx_lagou_position, knx_dajie_position, knx_qiancheng_position]:
        for item in db.find():
            db.update_one({'_id': item['_id']}, {'$set': {'priority': 0}})
